package xyz.cerberu.cet.spider.anime.spider;

import com.spider.core.SpiderClient;
import com.spider.core.SpiderConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xyz.cerberu.cet.spider.anime.spider.Logic.AnimeLogic;
import xyz.cerberu.cet.spider.anime.spider.Logic.HomeLogic;

import java.time.LocalDate;

/**
 * @author β世界
 * Created on 15:42
 * @Description 动漫的爬虫
 */
@Component
public class AnimeSpider {
    private final SpiderConfig animeSpiderConfig;
    private final AnimeLogic animeLogic;
    private final HomeLogic homeLogic;

    private static final String PREFIX_URL = "https://bgm.tv/anime/tag/TV/airtime/";

    @Autowired
    public AnimeSpider(SpiderConfig animeSpiderConfig, AnimeLogic animeLogic,HomeLogic homeLogic) {
        this.animeSpiderConfig = animeSpiderConfig;
        this.animeLogic = animeLogic;
        this.homeLogic = homeLogic;
    }

    /**
     * 获取这个季度动漫的更新情况
     * @author β世界 on 14:55 2021/5/25
     * @return void
     **/
    @Scheduled(cron = "0 0 2 * * ?") // 每天查看一次动漫的更新情况
    public void updateAnime(){
        SpiderClient client = animeSpiderConfig.create();
        // 设置代理
        client.addProcessLogic(animeLogic);
        client.addProcessLogic(homeLogic);

        LocalDate localDate = LocalDate.now();

        client.addSeedUrl(PREFIX_URL+localDate.getYear()+"-"+localDate.getMonthValue());

        client.start(); // 爬取开始

    }

    /**
     *指定日期来更新
     * @author β世界 on 15:41 2020/4/27
     * @return void
     **/
    public void assignTimeUpdateAnime(int year , int mouth){
        SpiderClient client = animeSpiderConfig.create();
        // 添加处理逻辑
        client.addProcessLogic(animeLogic);
        client.addProcessLogic(homeLogic);

        client.addSeedUrl(PREFIX_URL+year+"-"+mouth);

        client.start(); // 爬取开始

    }

}
